<?php
/**
 * Created by JetBrains PhpStorm.
 * User: zhouyang
 * Date: 13-7-15
 * Time: 下午4:31
 * To change this template use File | Settings | File Templates.
 */
class CategorysDB  extends DBReader{
    //根据category_id 获取 category 信息
    public function get($category_id){

        $sql = 'select category_id, en_name, zh_name, sort from categorys where category_id = :category_id';

        return $this->db()->getRow($sql,array('category_id'=>$category_id));
    }

    //根据category_id 获取 category 信息
    public function getCategoryInfoByName($category){

        $sql = 'select category_id, en_name, zh_name, sort from categorys where zh_name = :zh_name';

        return $this->db()->getRow($sql,array('zh_name'=>$category));
    }


    //获取来源信息列表<带分页>
    public function getCategoryList($curr_page,$per_page,$filter = array()){
        $where = ' 1=1 ';
        $bindarr = array();

        if(isset($filter['zh_name'])){
            $where .= ' and zh_name like :zh_name';
            $bindarr['zh_name'] = '%'.$filter['zh_name'].'%';
        }

        $ret = $this->pager($curr_page, $per_page)
            ->ct('select count(category_id) from categorys where '.$where,$bindarr)
            ->rs('select category_id, en_name, zh_name, sort from categorys where '.$where.' order by category_id desc', $bindarr);
        return $ret;
    }

    //获取所有来源信息
    public function getCategorys($filter = array(),$flag = false){
        $where = ' 1=1 ';
        $bindarr = array();

        if(isset($filter['zh_name'])){
            $where .= ' and zh_name like :zh_name';
            $bindarr['zh_name'] = '%'.$filter['zh_name'].'%';
        }

        $sql = 'select category_id, en_name, zh_name, sort from categorys where '. $where .' order by category_id desc';

        $ret = $this->db()->getAll($sql,$bindarr);

        if($flag == false){
            return $ret;
        }else{
            $new_ret = array();

            foreach((array)$ret as $k=>$v){
                $new_ret[$v['category_id']] = $v['zh_name'];
            }

            return $new_ret;
        }
    }

    //添加来源信息
    public function addCategory($en_name, $zh_name){

        $sql = "insert into categorys(en_name,zh_name,sort) values(:en_name,:zh_name,:sort)";

        $bindarr = array(
            'en_name'=>$en_name,
            'zh_name'=>$zh_name,
            'sort'=>0
        );
        $this->db()->execute($sql,$bindarr);
        return $this->db()->lastInsertId();
    }

    //删除来源信息
    public function delCategory($category_id){

        $sql = "delete from categorys where category_id = :category_id";

        $bindarr = array(
            'category_id'=>$category_id,
        );
        $rs = $this->db()->execute($sql,$bindarr);

        return $rs->rowCount();
    }

    //更新来源信息
    public function updateCategory($id,$en_name = null,$zh_name = null){

        $bindarr = array(
            'category_id'=>$id,
            'en_name'=>$en_name,
            'zh_name'=>$zh_name
        );

        $sql = "update categorys set en_name = :en_name, zh_name = :zh_name where category_id = :category_id";

        $rs = $this->db()->execute($sql,$bindarr);
        return $rs->rowCount();
    }
}